package day52.system.controller;

import day52.basic.annotation.PreAuthorize;
import day52.system.service.IRoleService;
import day52.system.domain.Role;
import day52.system.query.RoleQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    /**
     * 保存和修改公用的
     * @param role 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Role role) {
        if ( role.getId() != null){
            roleService.update(role);
        }else{
            roleService.add(role);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        roleService.delete(id);
        return AjaxResult.me();
    }

    //获取用户
    @GetMapping("/{id}")
    public Role get(@PathVariable("id") Long id) {
        return roleService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Role> list() {
        return roleService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "角色列表",value = "role:list") //value后面用来做权限校验
    public PageList<Role> json(@RequestBody RoleQuery query) {
        return roleService.pageQuery(query);
    }
}
